package cn.ecut.dzy.lock;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @program: juc
 * @description: 集合不安全问题
 * @author: DingZhenYun
 * @create: 2021-10-18 23:01
 **/

public class ThreadDemo4 {


    public static void main(String[] args) {
        ThreadDemo4 threadDemo4 = new ThreadDemo4();
        //threadDemo4.f1();
        //threadDemo4.f2();
        //threadDemo4.f3();
        //threadDemo4.f4();
//        threadDemo4.f5();
        threadDemo4.f6();

    }


    public void f1() {
        // 创建ArrayList集合--报错
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            new Thread(() -> {
                //向集合添加内容
                list.add(UUID.randomUUID().toString().substring(0, 8));
                //从集合获取内容
                System.out.println(list);
            }, String.valueOf(i)).start();
        }
    }


    public void f2() {
        List<String> list = new Vector<>();
        for (int i = 0; i < 30; i++) {
            new Thread(() -> {
                //向集合添加内容
                list.add(UUID.randomUUID().toString().substring(0, 8));
                //从集合获取内容
                System.out.println(list);
            }, String.valueOf(i)).start();
        }
    }


    public void f3() {
        //Collections解决
        List<String> list = Collections.synchronizedList(new ArrayList<>());
        for (int i = 0; i < 30; i++) {
            new Thread(() -> {
                //向集合添加内容
                list.add(UUID.randomUUID().toString().substring(0, 8));
                //从集合获取内容
                System.out.println(list);
            }, String.valueOf(i)).start();
        }
    }


    public void f4() {
        //CopyOnWriteArrayList解决
        List<String> list = new CopyOnWriteArrayList<>();
        for (int i = 0; i < 30; i++) {
            new Thread(() -> {
                //向集合添加内容
                list.add(UUID.randomUUID().toString().substring(0, 8));
                //从集合获取内容
                System.out.println(list);
            }, String.valueOf(i)).start();
        }
    }

    public void f5(){
        //Set<String> set = new HashSet<>();

       Set<String> set = new CopyOnWriteArraySet<>();
        for (int i = 0; i <30; i++) {
            new Thread(()->{
                //向集合添加内容
                set.add(UUID.randomUUID().toString().substring(0,8));
                //从集合获取内容
                System.out.println(set);
            },String.valueOf(i)).start();
        }
    }

    public void f6(){
        //演示HashMap
//        Map<String,String> map = new HashMap<>();

        Map<String,String> map = new ConcurrentHashMap<>();
        for (int i = 0; i <30; i++) {
            String key = String.valueOf(i);
            new Thread(()->{
                //向集合添加内容
                map.put(key,UUID.randomUUID().toString().substring(0,8));
                //从集合获取内容
                System.out.println(map);
            },String.valueOf(i)).start();
        }
    }
}
